/**
 * 采购审核purchaseApplyCheck.js
 * 版本号： v1.0.0
 */
$(function(){

    /**
     * 时间选中事件
     */
    $("#startDate").datebox({
        onSelect : function(beginDate){
            $('#endDate').datebox().datebox('calendar').calendar({
                validator: function(date){
                    return beginDate<=date;
                }
            });
        }
    });

    var lastChecked = null;
    $("#purchaseApplyCheck_dataList").datagrid({
        fitColumns: true,
        fit:true,
        method: "POST",
        rownumbers: true,
        singleSelect:true,
        pagination: true,
        striped: true,
        border: false,
        pageSize:10,
        pageList:[10,30,50,100],
        url: CONTEXT_PATH + '/purchaseApplyCheck/checkList.do',
        idField: 'purId',
        frozenColumns:[[
            {field:'purId',checkbox:true}
        ]],
        columns: [
            [
                {field: 'project', title: '项目名称', width: 180, formatter:function(val){
                        if(val){
                            return val.prName;
                        }
                    }},
                {field: 'contract', title: '合同名称', width: 180, fixed: true, formatter:function(val){
                        if(val){
                            return val.coName;
                        }
                    }},
                {field: 'contract_number', title: '合同编号', width: 160, fixed: true, formatter:function(val,row){
                        if(row.contract){
                            return row.contract.coNumber;
                        }
                    }},
                {field: 'purDate', title: '请购日期', width: 90, fixed: true, align:'center'},
                {field: 'reportState', title: '上报状态', width: 70, fixed: true, align:'center', formatter:function(val){
                        if (val == 1){
                            return "<span style='color: red; font-weight: bold'>未上报</span>";
                        }
                        if (val == 2){
                            return "<span style='color: green; font-weight: bold'>已上报</span>";
                        }
                    }},
                {field: 'checkState', title: '审核状态', width: 70, fixed: true, align:'center',formatter:function(val){
                        if (val == 0){
                            return "<span style='color: #2c24ff; font-weight: bold'>未审核</span>";
                        }
                        if (val == 1){
                            return "<span style='color: deepskyblue; font-weight: bold'>审核中</span>";
                        }
                        if (val == 2){
                            return "<span style='color: green; font-weight: bold'>已通过</span>";
                        }
                        if (val == 3){
                            return "<span style='color: red; font-weight: bold'>已驳回</span>";
                        }
                    }},
                {field: 'purType', title: '请购类型', width: 70, fixed: true, align:'center', formatter:function(val){
                        if (val == 1){
                            return "合同内";
                        }
                        if (val == 2){
                            return "合同外";
                        }
                    }},
                {field: 'purPerson', title: '请购人', width: 80, fixed: true}
            ]
        ],
        onCheck:function(rowIndex, rowData){
            var row = $("#purchaseApplyCheck_dataList").datagrid("getChecked");
            if(row && rowIndex == lastChecked){
                $('#purchaseApplyCheck_dataList').datagrid("unselectAll");//取消选中当前所有行
                lastChecked = null;
            }else{
                lastChecked = rowIndex;
            }
        },
        onLoadSuccess:function(data){
            if(data.rows.length > 0){
                lastChecked = null;
                $("#purchaseApplyCheck_dataList").datagrid("selectRow", 0);
            }
        }
    });

    //定义可操作按钮
    $("#purchaseApplyCheck_Menu").dialog({
        title: '',
        width: 'auto',
        height:35,
        closed: false,
        cache: false,
        inline: true,
        modal: false,
        border: false,
        toolbar: createToolbar()
    });

});

/**
 * 创建按钮
 * @returns {Array}
 */
function createToolbar() {
    var toolbar = [];
    var falg = false;
    for(var j = 0; j < btnOptions.length; j++ ){
        if(false != btnOptions[j].value){
            falg = true;
            break;
        }
    }
    if(!falg){
        //不存在按钮
        toolbar.push({});
    }else{
        //存在按钮
        for(var i = 0; i < btnOptions.length; i++){
            if(false != btnOptions[i].value){
                //查询按钮
                if("searchBtn"  == btnOptions[i].key){
                    toolbar.push({
                        id:btnOptions[i].value.meId,
                        text:btnOptions[i].value.name,
                        iconCls:btnOptions[i].value.iconCls,
                        handler:function(){
                            purchaseCheck_findList();
                        }
                    });
                }
                //采购对比查看按钮
                if("contrastBtn" == btnOptions[i].key){
                    toolbar.push({
                        id:btnOptions[i].value.meId,
                        text:btnOptions[i].value.name,
                        iconCls:btnOptions[i].value.iconCls,
                        handler:function(){
                            contrast_list();
                        }
                    });
                }
                //合同剩余量查看按钮
                if("countLeftBtn" == btnOptions[i].key){
                    toolbar.push({
                        id:btnOptions[i].value.meId,
                        text:btnOptions[i].value.name,
                        iconCls:btnOptions[i].value.iconCls,
                        handler:function(){
                            countLeft_list();
                        }
                    });
                }
            }
        }
    }
    return toolbar;
}

/**
 * 查询
 */
function purchaseCheck_findList(){
    refreshPageNumber("purchaseApplyCheck_dataList");
    $("#purchaseApplyCheck_dataList").datagrid("reload", {
        prName: $("#prName").textbox('getValue'),
        coName: $("#coName").textbox('getValue'),
        coNumber: $("#coNumber").textbox('getValue'),
        purType: $("#purType").combobox('getValue'),
        startDate: $("#startDate").datebox('getValue'),
        endDate: $("#endDate").datebox('getValue'),
        checkState:$("#checkState").combobox('getValue')
    });
    $("#purchaseApplyCheck_dataList").datagrid("clearSelections");
}

/**
 * 采购对比查看
 */
function contrast_list(){
    var checked = $("#purchaseApplyCheck_dataList").datagrid("getChecked");
    if (!checked || !checked.length) {
        $.messager.show({ title: '操作提示', msg: "请选择需要查看的数据！", timeout: 3000, showType: "slide" });
        return;
    }
    var row = $("#purchaseApplyCheck_dataList").datagrid("getSelected");
    window.location.href = CONTEXT_PATH + "/purchaseApplyCheck/toContrast.do?purId="+row.purId;
}

/**
 * 合同剩余量查看
 */
function countLeft_list(){
    var checked = $("#purchaseApplyCheck_dataList").datagrid("getChecked");
    if (!checked || !checked.length) {
        $.messager.show({ title: '操作提示', msg: "请选择需要查看的数据！", timeout: 3000, showType: "slide" });
        return;
    }
    var row = $("#purchaseApplyCheck_dataList").datagrid("getSelected");
    window.location.href = CONTEXT_PATH + "/purchaseApplyCheck/toCountLeft.do?purId="+row.purId;
}

